// 最小化测试文件 - 逐步添加代码以定位错误
Page({
  data: {
    // 基本数据
    currentDate: '',
    selectedDepartment: '全部科室',
    morningAppointments: [],
    afternoonAppointments: [],
    eveningAppointments: [],
    
    // 统计数据
    totalAppointments: 0,
    pendingAppointments: 0,
    confirmedAppointments: 0,
    completedAppointments: 0,
    cancelledAppointments: 0,
    
    // 操作确认弹窗
    showActionConfirm: false,
    actionType: '',
    currentAppointmentId: '',
    
    // 医生备注功能
    showNotePopup: false,
    noteCurrentAppointmentId: '', // 重命名以避免冲突
    currentPatientName: '',
    currentPatientTime: '',
    currentNote: '',
    quickNotes: ['需要详细检查', '慢性病复诊', '紧急情况', '预约手术', '咨询用药']
  },
  
  // 计算统计数据
  calculateStatistics() {
    // 合并所有时段的预约数据
    const allAppointments = [...this.data.morningAppointments, ...this.data.afternoonAppointments, ...this.data.eveningAppointments];
    
    // 模拟不同状态的预约数据
    const mockStats = {
      totalAppointments: allAppointments.length,
      pendingAppointments: Math.floor(allAppointments.length * 0.5),
      confirmedAppointments: Math.floor(allAppointments.length * 0.3),
      completedAppointments: Math.floor(allAppointments.length * 0.15),
      cancelledAppointments: Math.max(0, allAppointments.length - Math.floor(allAppointments.length * 0.95))
    };
    
    this.setData(mockStats);
    
    // 隐藏加载提示
    wx.hideLoading();
  },
  
  // 显示医生备注弹窗
  showNotePopup(e) {
    const id = e.currentTarget.dataset.id;
    let appointment = null;
    
    [this.data.morningAppointments, this.data.afternoonAppointments, this.data.eveningAppointments].forEach(list => {
      const found = list.find(item => item.id === id);
      if (found) {
        appointment = found;
      }
    });
    
    if (appointment) {
      this.setData({
        showNotePopup: true,
        noteCurrentAppointmentId: id,
        currentPatientName: appointment.name,
        currentPatientTime: appointment.time,
        currentNote: appointment.note || ''
      });
    }
  },
  
  // 保存备注
  saveNote() {
    const { noteCurrentAppointmentId, currentNote } = this.data;
    
    // 更新对应的预约备注
    const updateAppointmentNote = (list) => {
      return list.map(item => {
        if (item.id === noteCurrentAppointmentId) {
          return { ...item, note: currentNote };
        }
        return item;
      });
    };
    
    this.setData({
      morningAppointments: updateAppointmentNote(this.data.morningAppointments),
      afternoonAppointments: updateAppointmentNote(this.data.afternoonAppointments),
      eveningAppointments: updateAppointmentNote(this.data.eveningAppointments),
      showNotePopup: false
    });
  }
});
